<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <style>
        :root {
            --primary-color: #2c3e50;
            --secondary-color: #3498db;
            --background-color: #ecf0f1;
            --text-color: #333;
            --error-color: #e74c3c;
            --success-color: #2ecc71;
            --info-color: #3498db;
        }

        body {
            font-family: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, 'Roboto', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background: linear-gradient(135deg, #3498db, #2c3e50);
            animation: gradient 15s ease infinite;
            background-size: 400% 400%;
        }

        @keyframes gradient {
            0% {
                background-position: 0% 50%;
            }
            50% {
                background-position: 100% 50%;
            }
            100% {
                background-position: 0% 50%;
            }
        }

        form {
            background-color: rgba(255, 255, 255, 0.98);
            padding: 3rem;
            border-radius: 1.5rem;
            box-shadow: 0 1.25rem 2.5rem rgba(0, 0, 0, 0.15);
            width: 100%;
            max-width: 420px;
            animation: fadeIn 0.6s cubic-bezier(0.39, 0.575, 0.565, 1);
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        h2 {
            text-align: center;
            color: var(--primary-color);
            margin-bottom: 2.5rem;
            font-weight: 700;
            font-size: 1.75rem;
            letter-spacing: -0.025em;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            color: var(--text-color);
            font-weight: 600;
            font-size: 0.9rem;
        }

        input, select {
            width: 100%;
            padding: 1rem;
            border: 2px solid #e0e0e0;
            border-radius: 0.75rem;
            font-size: 1rem;
            transition: all 0.3s ease;
            box-sizing: border-box;
        }

        input:focus, select:focus {
            outline: none;
            border-color: var(--secondary-color);
            box-shadow: 0 0 0 4px rgba(52, 152, 219, 0.15);
        }

        input:valid:not(:placeholder-shown) {
            border-color: var(--success-color);
        }

        input:invalid:not(:placeholder-shown) {
            border-color: var(--error-color);
        }

        .error-message {
            color: var(--error-color);
            margin-top: 0.5rem;
            font-size: 0.8rem;
            display: none;
        }

        input:invalid:not(:placeholder-shown) + .error-message {
            display: block;
        }

        input[type="submit"] {
            background-color: var(--secondary-color);
            color: white;
            border: none;
            padding: 1.125rem;
            border-radius: 0.75rem;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 600;
            transition: all 0.3s ease;
            margin-top: 1rem;
        }

        input[type="submit"]:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        input[type="submit"]:active {
            transform: translateY(0);
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
        }

        input[type="submit"]:disabled {
            background-color: #a0cfff;
            cursor: not-allowed;
            box-shadow: none;
        }

        a {
            display: block;
            text-align: center;
            color: var(--secondary-color);
            text-decoration: none;
            margin-top: 1.5rem;
            font-size: 0.9rem;
            transition: color 0.3s ease;
        }

        a:hover {
            color: #2980b9;
            text-decoration: underline;
        }

        .loading {
            position: relative;
        }

        .loading::after {
            content: "";
            position: absolute;
            right: 1.25rem;
            top: 50%;
            width: 1.25rem;
            height: 1.25rem;
            border: 3px solid #fff;
            border-radius: 50%;
            border-top-color: transparent;
            animation: spin 1s linear infinite;
            transform: translateY(-50%);
        }

        @keyframes spin {
            to {
                transform: translateY(-50%) rotate(360deg);
            }
        }
    </style>
</head>
<body>
<form th:action="@{/user/register}" method="post" onsubmit="handleSubmit(event)">
    <h2>欢迎注册</h2>
    <span id="registerErrorMsg" th:text="${msg}" style="color: red;font-size: 18px" ></span>
    <div class="form-group">
        <label for="username">用户名</label>
        <input type="text" id="username" name="username" required placeholder="请输入您的用户名">
        <div class="error-message">请输入有效的用户名</div>
    </div>
    <div class="form-group">
        <label for="password">密码</label>
        <input type="password" id="password" name="passwordHash" required placeholder="请输入您的密码">
        <div class="error-message">请输入有效的密码</div>
    </div>
    <div class="form-group">
        <label for="email">邮箱</label>
        <input type="email" id="email" name="email" required placeholder="请输入您的邮箱">
        <div class="error-message">请输入有效的邮箱地址</div>
    </div>
    <div class="form-group">
        <label for="phone">电话</label>
        <input type="tel" id="phone" name="phone" placeholder="请输入您的电话">
        <div class="error-message">请输入有效的电话号码</div>
    </div>
    <div class="form-group">
        <label for="role">角色</label>
        <select id="role" name="role" onchange="toggleDeliveryArea()">
            <option value="" disabled selected>请选择角色</option>
            <option value="user">普通用户</option>
            <option value="courier">骑手</option>
        </select>
    </div>
    <!-- 新增配送区域选择框，默认隐藏 -->
    <div class="form-group" id="deliveryAreaGroup" style="display: none;">
        <label for="deliveryArea">配送区域</label>
        <select id="deliveryArea" name="deliveryArea">
            <option value="0" disabled selected>请选择配送区域</option>
            <option th:each="area : ${deliveryAreas}" th:value="${area.regionId}" th:text="${area.name}"></option>
        </select>
    </div>
    <input type="submit" value="注册" id="submitButton">
    <a th:href="@{/user/login}">已有账号？登录</a>
</form>
<script>
    function handleSubmit(event) {
        const submitButton = document.getElementById('submitButton');
        const errorMessages = document.querySelectorAll('.error-message');
        errorMessages.forEach(msg => msg.style.display = 'none');

        submitButton.disabled = true;
        submitButton.classList.add('loading');

        window.addEventListener('load', function () {
            const registerErrorMsg = document.getElementById('loginErrorMsg');
            if (registerErrorMsg) {
                setTimeout(() => {
                    registerErrorMsg.style.display = 'none';
                },3000);
            }
        });


        // 这里可以添加实际的表单验证和提交逻辑
        // 示例：模拟异步请求
        setTimeout(() => {
            submitButton.disabled = false;
            submitButton.classList.remove('loading');
        }, 300);
    }
    // 新增函数，根据角色选择显示或隐藏配送区域选择框
    function toggleDeliveryArea() {
        const roleSelect = document.getElementById('role');
        const deliveryAreaGroup = document.getElementById('deliveryAreaGroup');
        if (roleSelect.value === 'courier') {
            deliveryAreaGroup.style.display = 'block';
        } else {
            deliveryAreaGroup.style.display = 'none';
        }
    }
</script>
</body>
</html>